home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / GLUT-3.7 / PROGS / DEMOS / GLIQ / BOARD.C next >
Encoding:
C/C++ Source or Header  |  1998-08-12  |  7.4 KB  |  344 lines

  1. /*  
  2.  *  CS 453 - Final project : An OpenGL version of the pegboard game IQ
  3.  *  Due : June 5, 1997
  4.  *  Author : Kiri Wagstaff
  5.  *
  6.  *  File : board.c
  7.  *  Description : Contains the board readin and selection functions.
  8.  *
  9.  *  
  10.  */
  11.  
  12. #include "gliq.h"
  13.  
  14. /* functions */
  15. void selectboard(void);
  16. void readboards(void);
  17. void drawboard(void);
  18. void drawpegs(void); /* Draw all the pegs */
  19. void drawpeg(void);  /* Draw one peg */
  20. void displaybuttons(void);
  21.  
  22. /* globals */
  23. int numboards, curboard;
  24. int*** boards;
  25. int filled[BOARDSIZE][BOARDSIZE]; /* Current state of the pegs */
  26.  
  27. /* Define the board */
  28. GLfloat vertices[8*3] = { 
  29.   -5.0,0.0,5.0, 
  30.   5.0,0.0,5.0,
  31.   5.0,0.5,5.0,
  32.   -5.0,0.5,5.0,
  33.   -5.0,0.0,-5.0,
  34.   5.0,0.0,-5.0,
  35.   5.0,0.5,-5.0,
  36.   -5.0,0.5,-5.0
  37. };
  38.  
  39. GLuint faces[6*4] = { 
  40.   0,1,2,3, /*front*/
  41.   0,3,7,4, /*left*/ 
  42.   0,4,5,1, /*bottom*/
  43.   1,5,6,2, /*right*/
  44.   3,2,6,7, /*top*/
  45.   4,7,6,5  /*back*/
  46. };
  47.  
  48. GLfloat normals[6*3] = {
  49.    0.0,  0.0,  1.0,
  50.   -1.0,  0.0,  0.0,
  51.    0.0, -1.0,  0.0,
  52.    1.0,  0.0,  0.0,
  53.    0.0,  1.0,  0.0,
  54.    0.0,  0.0, -1.0,
  55. };
  56.  
  57.  
  58. void selectboard(void)
  59. {
  60.   int height=glutGet(GLUT_WINDOW_HEIGHT);
  61.   int width=glutGet(GLUT_WINDOW_WIDTH);
  62.   static float spin=0.0;
  63.  
  64.   /* Eventually make it spin */
  65.   /* Display the buttons */
  66.   displaybuttons();
  67.   /* Draw the quit button */
  68.   drawquit(7.0, 9.0, 0.4, 1.0);
  69.   /* Quit */
  70.   glColor3f(1.0, 1.0, 1.0);  /* white */
  71.   /*  text(0.78*width, 0.89*height, 0.1*height, "Quit"); */
  72.   /* Select message */
  73.   glColor3f(0.0, 1.0, 0.0);
  74.   text(0.3*width, 0.9*height, 0.07*height, "Select a board");
  75.  
  76.   /* Draw the total # of pegs */
  77.   glPushMatrix();
  78.     glColor3f(1.0, 1.0, 0.0);     /* yellow */
  79.     glTranslatef(-7.8, 8.8, 0.0);
  80.     drawpeg();
  81.     text(0.1*width, 0.9*height, 0.07*height, ": %02d", totalpegs);
  82.   glPopMatrix();
  83.   
  84.   /* do the trackball rotation. */
  85.   glPushMatrix();
  86.   /*    tbMatrix(); */
  87.     glRotatef(45.0, 1.0, 0.0, 0.0);
  88.     glRotatef(spin, 0.0, 1.0, 0.0);
  89.     drawboard();
  90.     drawpegs();
  91.   glPopMatrix();
  92.   spin++;
  93. }
  94.  
  95. void readboards(void)
  96. {
  97.   int i, j, hole;
  98.   FILE* fp;
  99.  
  100.   /* Read in the boards */
  101.   fp = fopen("boards.txt", "r");
  102.   if (!fp)
  103.     {
  104.       printf("Could not open boards.txt, exiting.\n");
  105.       exit(1);
  106.     }
  107.   fscanf(fp, "%d", &numboards);
  108.   boards = (int***)malloc(numboards*sizeof(int**));
  109.   for (i=0; i<numboards; i++)
  110.     {
  111.       boards[i] = (int**)malloc(BOARDSIZE*sizeof(int*));
  112.       for (j=0; j<BOARDSIZE; j++)
  113.     boards[i][j] = (int*)malloc(BOARDSIZE*sizeof(int));
  114.     }
  115.   for (i=0; i<numboards; i++)
  116.     for (j=0; j<BOARDSIZE*BOARDSIZE; j++)
  117.       {
  118.     fscanf(fp, "%d", &hole);
  119.     boards[i][j/BOARDSIZE][j%BOARDSIZE] = hole;
  120.       }
  121.   totalpegs = 0;
  122.   /* Set up filled array */
  123.   for (i=0; i<BOARDSIZE; i++)
  124.     for (j=0; j<BOARDSIZE; j++) 
  125.       {
  126.     filled[i][j] = boards[curboard][i][j];
  127.     if (filled[i][j] == FULL)
  128.       totalpegs++;
  129.       }
  130.  
  131.   if (numboards == 1)
  132.     {
  133.       curboard = 0;
  134.       pegs = totalpegs;
  135.       curstate = PLAY;
  136.     }
  137. }
  138.  
  139. void drawboard(void)
  140. {
  141.   int i, j;
  142.   GLUquadricObj* hole;
  143.  
  144.   /* Draw the board */
  145.   glColor3f(0.3, 0.3, 1.0);  /* Blue */
  146.   glShadeModel(GL_FLAT);
  147.   glBegin(GL_QUADS);
  148.   for (i=0; i<6; i++) 
  149.     {
  150.       glNormal3fv(&normals[3*i]);
  151.       for (j=0; j<4; j++) 
  152.     glVertex3fv(&vertices[3*faces[i*4 + j]]);
  153.     }
  154.   glEnd();
  155.  
  156.   /* Draw holes */
  157.   glShadeModel(GL_SMOOTH);
  158.   /*  glColor3f(0.0, 0.0, 0.0); */
  159.   glPushMatrix();
  160.   glTranslatef(-4.0, 0.51, -4.0);
  161.   for (i=0; i<BOARDSIZE; i++)
  162.     {
  163.       glPushMatrix();
  164.       for (j=0; j<BOARDSIZE; j++)
  165.     {
  166.       if (filled[i][j] == UNUSED)
  167.         {
  168.           glTranslatef(1.0, 0.0, 0.0);
  169.           continue;
  170.         }
  171.       glColor3f(0.3, 0.3, 1.0);  /* Blue */
  172.       glPushMatrix();
  173.         glRotatef(-90.0, 1.0, 0.0, 0.0);
  174.         hole = gluNewQuadric();
  175.         gluQuadricDrawStyle(hole, GLU_FILL);
  176.         gluQuadricNormals(hole, GLU_SMOOTH);
  177.         gluCylinder(hole, 0.3, 0.3, 0.5, 8, 1); 
  178.         gluDeleteQuadric(hole);
  179.       glPopMatrix();
  180.       glTranslatef(1.0, 0.0, 0.0);
  181.     }
  182.       glPopMatrix();
  183.       glTranslatef(0.0, 0.0, 1.0);
  184.     }
  185.   glPopMatrix();
  186. }
  187.  
  188. void drawpegs(void)
  189. {
  190.   int i, j;
  191.   int name = 0;
  192.   
  193.   /* Draw pegs */
  194.   glShadeModel(GL_SMOOTH);
  195.   glColor3f(1.0, 1.0, 0.0);  /* Yellow */
  196.  
  197.   glPushMatrix();
  198.     glTranslatef(-4.0, 0.51, -4.0);
  199.     for (i=0; i<BOARDSIZE; i++)
  200.       {
  201.     glPushMatrix();
  202.     for (j=0; j<BOARDSIZE; j++)
  203.       {
  204.         name++;
  205.         switch (filled[i][j])
  206.           {
  207.           case EMPTY:
  208.         glLoadName(name);
  209.         glDepthMask(GL_FALSE);
  210.         glEnable(GL_BLEND);
  211.         glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
  212.         glColor4f(1.0, 1.0, 0.0, 0.0);  /* Invisible */
  213.         drawpeg();
  214.         glDisable(GL_BLEND);
  215.         glDepthMask(GL_TRUE);
  216.         break;
  217.           case UNUSED:
  218.         glTranslatef(1.0, 0.0, 0.0);
  219.         continue;
  220.           case FULL:
  221.         glLoadName(name);
  222.         glColor3f(1.0, 1.0, 0.0);  /* Yellow */
  223.         if (picked == name)
  224.           glColor3f(1.0, 0.5, 0.0); /* Orange */
  225.         drawpeg();
  226.         break;
  227.           case CANMOVE:
  228.         glLoadName(name);
  229.         glColor3f(0.0, 1.0, 0.0); /* Green */
  230.         drawpeg();
  231.         break;
  232.           case CANTMOVE:
  233.         glLoadName(name);
  234.         glColor3f(1.0, 0.0, 0.0); /* Red */
  235.         drawpeg();
  236.         break;
  237.           default:
  238.         printf("Unknown peg value %d, exiting.", filled[i][j]);
  239.         exit(1);
  240.           }
  241.         glTranslatef(1.0, 0.0, 0.0);
  242.       }
  243.     glPopMatrix();
  244.     glTranslatef(0.0, 0.0, 1.0);
  245.       }
  246.   glPopMatrix();
  247. }
  248.  
  249.  
  250. void drawpeg(void)
  251. {
  252.   float ang=-90.0;
  253.   float radcyl=0.25;
  254.   float radball=0.4;
  255.   float len=0.8;
  256.   static GLuint peg=0;
  257.   GLUquadricObj* stick;
  258.  
  259.   /* Generate the displaylist on the first call */ 
  260.   if (peg) 
  261.     {
  262.       glCallList(peg);
  263.       return;
  264.     }
  265.  
  266.   peg = glGenLists(1);
  267.   glNewList(peg, GL_COMPILE_AND_EXECUTE);
  268.  
  269.   /* Draw the ball */
  270.   glPushMatrix();
  271.     glTranslatef(0.0, len+(radball/2), 0.0);
  272.     glutSolidSphere(radball, 8, 8);
  273.   glPopMatrix();
  274.  
  275.   /* Draw the cone (stick) */
  276.   /*  glColor3f(1.0, 1.0, 0.0);  Yellow */
  277.   stick = gluNewQuadric();
  278.   glPushMatrix();
  279.     glRotatef(ang, 1.0, 0.0, 0.0);
  280.     gluQuadricDrawStyle(stick, GLU_FILL);
  281.     gluQuadricNormals(stick, GLU_SMOOTH);
  282.     gluCylinder(stick, radcyl, radcyl, len, 8, 1); 
  283.     gluDeleteQuadric(stick);
  284.     /*    glutSolidCone(rad, len, 8, 8);*/
  285.   glPopMatrix();
  286.   glEndList();
  287. }
  288.  
  289.  
  290. void displaybuttons(void)
  291. {
  292.   GLUquadricObj* stick;
  293.  
  294.   /* Previous*/
  295.   glPushMatrix();
  296.     glLoadName(LEFTARR);
  297.     if (picked == LEFTARR)
  298.       glColor3f(1.0, 1.0, 1.0);  /* white */
  299.     else
  300.       glColor3f(0.0, 1.0, 0.0);  /* green */
  301.  
  302.     glTranslatef(-5.0, 6.5, -2.0);
  303.     glRotatef(-90.0, 0.0, 1.0, 0.0);
  304.     glutSolidCone(1.5, 3.0, 8, 8);
  305.     glTranslatef(0.0, 0.0, -2.0);
  306.     stick = gluNewQuadric();
  307.     gluQuadricDrawStyle(stick, GLU_FILL);
  308.     gluQuadricNormals(stick, GLU_SMOOTH);
  309.     gluCylinder(stick, 1.0, 1.0, 2.0, 8, 1); 
  310.     gluDeleteQuadric(stick);
  311.   glPopMatrix();
  312.   /* Select */
  313.   glPushMatrix();
  314.     glLoadName(SELECT);
  315.     if (picked == SELECT)
  316.       glColor3f(1.0, 1.0, 1.0);  /* white */
  317.     else
  318.       glColor3f(0.0, 1.0, 0.0);  /* green */
  319.  
  320.     glTranslatef(0.0, 6.5, -2.0);
  321.     glutSolidCube(2.5);
  322.   glPopMatrix();
  323.   /* Next */
  324.   glPushMatrix();
  325.     glLoadName(RIGHTARR);
  326.     if (picked == RIGHTARR)
  327.       glColor3f(1.0, 1.0, 1.0);  /* white */
  328.     else
  329.       glColor3f(0.0, 1.0, 0.0);  /* green */
  330.  
  331.     glTranslatef(5.0, 6.5, -2.0);
  332.     glRotatef(90.0, 0.0, 1.0, 0.0);
  333.     glutSolidCone(1.5, 3.0, 8, 8);
  334.     glTranslatef(0.0, 0.0, -2.0);
  335.     stick = gluNewQuadric();
  336.     gluQuadricDrawStyle(stick, GLU_FILL);
  337.     gluQuadricNormals(stick, GLU_SMOOTH);
  338.     gluCylinder(stick, 1.0, 1.0, 2.0, 8, 1); 
  339.     gluDeleteQuadric(stick);
  340.   glPopMatrix();
  341.  
  342.   glLoadName(0); /* stop name loading */
  343. }  
  344.